package com.controller;

import com.dao.DatasetDao;
import com.entity.UserMetadata;
import com.service.DatasetOperator;
import com.util.CookieUtil;
import com.util.FileUtil;
import com.util.LocalInformation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.util.PermissionUtil.isDATASETDownload;

@Controller
@RequestMapping(value = "/dataset_download", produces = "text/html;charset=UTF-8")
public class DatasetDownloadController {

    @RequestMapping(value = "/download_list_page", produces = "text/html;charset=UTF-8")
    public ModelAndView download_list_page(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)) {
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());
            if (isDATASETDownload(user_cookie_value.split("wlcniubi")[2])) {
                //进入数据导入界面
                modelAndView.addObject("id", LocalInformation.getTimeID());
                modelAndView.setViewName("dataset-download");
            } else {
                modelAndView.setViewName("Permission-Denied");
            }
        } else {
            modelAndView.setViewName("login");
        }

        return modelAndView;
    }


    @RequestMapping(value = "/get_dataset_list", produces = "text/html;charset=UTF-8")
    public void get_dataset_list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());
        String projectPath = request.getSession().getServletContext().getRealPath("/");

        response.setContentType("text/html;charset=utf-8");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.println(DatasetOperator.getDataSet());
        out.flush();
        out.close();
    }


    @RequestMapping(value = "/dataset_download", produces = "text/html;charset=UTF-8")
    public void template_download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filePath = DatasetDao.getFilePathByName(request.getParameter("dataset_name"));
        System.out.println(request.getParameter("dataset_name"));
        System.out.println("filePath:" + filePath);
        if (filePath == null || filePath.equals(""))
            response.sendRedirect("/db/dataset_download/download_list_page");
        else
            FileUtil.file_remote_download(filePath, null, response);


    }
}
